#!/usr/bin/env python
#coding=utf-8

import urllib, urllib2
import cookielib
import re, time
import threading

class Venue():
    headers = { 'User-Agent' : """Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 GTB6"""}
    headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
    headers['Accept-Encoding'] = 'gzip,deflate'
    headers['Accept-Language'] = "zh,en-us;q=0.7,en;q=0.3"
    headers['Accept-Charset'] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
    headers['Connection'] = "keep-alive"
    headers['Keep-Alive'] = "115"
    headers['Cache-Control'] = "no-cache"
    v_name = re.compile(r"""<div class="CommonTitle01 VenueTitle">(.*?)<div""", re.S)
    v_addr = re.compile(r"""<div class="Addr">.*?nbsp;(.*?)</div>""", re.S)
    v_phone = re.compile(r"""<div class="PhoneNumber">.*?nbsp;(\d+)</div>""", re.S)
    v_point = re.compile(r"""<script.*initMap\(([\d\.]+),([\d\.]+),\d+""", re.S)

    def _opener(self):
        """
        构建自动处理httpcookie的opener
        """
        cj = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj), urllib2.HTTPHandler())
        return opener

    def _wrap(self, url, param=None):
        return urllib2.Request(url, param, self.headers)

    def __init__(self, base ,id):
        self.id = id
        self.base = base
    
    def fetch(self):
        op = self._opener()
        try:
            r = op.open(self._wrap(self.base+self.id))
            self.content = r.read()            
            r.close()
        except:
            print "error occured"

    def parse(self):
        if not hasattr(self, "content"):return ()

        n = self.v_name.search(self.content)
        if not n: return ()
        
        a = self.v_addr.search(self.content)
        ph = self.v_phone.search(self.content)
        po = self.v_point.search(self.content)
        n.group(1).decode("utf-8").strip().replace("\r","")

        name = n.group(1).decode("utf-8").strip().replace("\r","")
        addr = ""
        if a: addr = a.group(1).decode("utf-8").strip().replace("\r","")
        phone = ""
        if ph: phone = ph.group(1).strip().replace("\r","")
        lat, lng = po.group(1).strip().replace("\r",""), po.group(2).strip().replace("\r","")

        return name, addr, phone, lat, lng

def fetch():
    f = open("./p4f.2.txt", 'wb+')
    
    tasks = []
    rets = []
    for i in range(220700, 420700):
        def wrap(tid):
            def run():
                try:
                    v = Venue("http://www.play4f.cn/venue/", str(tid))
                    v.fetch()
                    rets.append((tid, v.parse()))
                except:pass
                print "r", tid
                tasks.remove(tid)
            return run

        tasks.append(i)
        t = threading.Thread(target = wrap(i))
        t.start()

        if len(tasks) > 60:
            time.sleep(1)
            while 1:
                if len(rets) > 0:
                    tid, r = rets.pop()
                    if r: 
                        print >>f,str(tid)+"\t"  +"\t".join(r)
                        f.flush()
                else:
                    break

    f.close()

if __name__=="__main__":
    #c = open("venue48642.htm",'rb').read()
    #v = Venue('','')
    #v.content = c
    #print "\t".join(v.parse())
    fetch()